﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Gw2Api
{
    public class GuildDetails
    {
        public string guild_id;
        public string guild_name;
        public string tag;

        public override string ToString()
        {
            return guild_name + " [" + tag + "]";
        }
    }

    public class GuildDetailsApi : BaseApi
    {
        private static Dictionary<string, GuildDetails> _guilds = new Dictionary<string, GuildDetails>();

        public static GuildDetails GetGuildDetails(string guildId)
        {
            return _guilds.ContainsKey(guildId) ? _guilds[guildId] : LoadGuildDetailsApi(guildId);
        }

        private static GuildDetails LoadGuildDetailsApi(string guildId)
        {
            string json = LoadFromUrl(ApiUrls.GuildDetails + "?guild_id=" + guildId);
            if (json.Contains("invalid guild_id") || json.Contains("\"text\":"))
            {
                return new GuildDetails { guild_id = guildId, guild_name = "Unknown Guild", tag = "?" };
            }
            _guilds[guildId] = JsonConvert.DeserializeObject<GuildDetails>(json);
            return _guilds[guildId];
        }
    }
}
